package com.libl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.libl.mapper.DtsBrandMapper;
import com.libl.pojo.DtsBrand;
import com.libl.pojo.DtsCategory;
import com.libl.service.DtsBrandService;
import com.libl.service.DtsCategoryService;
import com.libl.vo.DtsBrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class DtsBrandServiceImpl implements DtsBrandService {
    @Autowired
    DtsBrandMapper brandMapper;

    @Override
    public List<Integer> findBrandIdsByAdminId(Integer id) {
        LambdaQueryWrapper<DtsBrand> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsBrand::getAdminId, id);
        wrapper.eq(DtsBrand::getDeleted, false);
        //指定只查询id这一列
        wrapper.select(DtsBrand::getId);

        List<DtsBrand> brandList = brandMapper.selectList(wrapper);

        //把id提取出来，用stream流转换
        List<Integer> brandIds = brandList.stream().map((brand) -> {
            return brand.getId();
        }).collect(Collectors.toList());

        return brandIds;
    }

    @Override
    public List<Map<String, Object>> findBrandList(List<Integer> brandIds) {
        LambdaQueryWrapper<DtsBrand> wrapper = new LambdaQueryWrapper<>();
        if(brandIds != null && brandIds.size() > 0){
            wrapper.in(DtsBrand::getId, brandIds);
        }

        List<DtsBrand> brandList = brandMapper.selectList(wrapper);

        //转换数据
        List<Map<String, Object>> data = brandList.stream().map((brand) -> {
            //一个品牌对应一个map集合
            Map<String, Object> map = new HashMap<>();
            map.put("value", brand.getId());
            map.put("label",brand.getName());
            return map;
        }).collect(Collectors.toList());

        return data;
    }

    @Override
    public DtsBrand selectById(Integer brandId) {
        return brandMapper.selectById(brandId);
    }

    @Autowired
    DtsCategoryService categoryService;
    @Override
    public Map<String, Object> findByPage(Integer page, Integer limit, String sort, String order, String name, Integer id, List<Integer> brandId) {
        //创建分页对象
        IPage<DtsBrand> iPage = new Page<>(page,limit);
        //创建条件查询对象
        QueryWrapper<DtsBrand> wrapper = new QueryWrapper<>();
        //先进行条件判断
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(id)){
            wrapper.like("id",id);
        }
        if(brandId != null && brandId.size() > 0){
            wrapper.in("id",brandId);
        }

        if("desc".equalsIgnoreCase(order)){
            wrapper.orderByDesc(sort);
        }else {
            wrapper.orderByAsc(sort);
        }
        //未被逻辑删除
        wrapper.eq("deleted",false);

        brandMapper.selectPage(iPage,wrapper);
        List<DtsBrand> brands = iPage.getRecords();
        //数据转换
        List<Integer> defaultCategoryIds = brands.stream().map(
                brand -> brand.getDefaultCategoryId()
        ).collect(Collectors.toList());

        List<DtsCategory> categoryList = categoryService.findByDefaultId(defaultCategoryIds);

        DtsBrandVo brandVo = new DtsBrandVo();
        List<Object> list = new ArrayList<>();
        //遍历
        for (DtsBrand brand : brands) {
            for (DtsCategory category : categoryList) {
                if(brand.getDefaultCategoryId().equals(category.getId())){
                    Integer[] categoryIds = new Integer[2];
                    categoryIds[0] = category.getPid();
                    categoryIds[1] = brand.getDefaultCategoryId();

                    brandVo.setCategoryIds(categoryIds);

                    HashMap<String,Object> map = new HashMap<>();
                    map.put("id",brand.getId());
                    map.put("name",brand.getName());
                    map.put("desc",brand.getDesc());
                    map.put("picUrl",brand.getPicUrl());
                    map.put("sortOrder",brand.getSortOrder());
                    map.put("floorPrice",brand.getFloorPrice());
                    map.put("addTime",brand.getAddTime());
                    map.put("updateTime",brand.getUpdateTime());
                    map.put("shareUrl",brand.getShareUrl());
                    map.put("adminId",brand.getAdminId());
                    map.put("deleted",brand.getDeleted());
                    map.put("commpany",brand.getCommpany());
                    map.put("autoUpdateGood",brand.getAutoUpdateGood());
                    map.put("shopUrl",brand.getShopUrl());
                    map.put("defaultCategoryId",brand.getDefaultCategoryId());
                    map.put("defaultPages",brand.getDefaultPages());
                    map.put("addPrecent",brand.getAddPrecent());
                    map.put("categoryIds",brandVo.getCategoryIds());

                    list.add(map);
                }
            }
        }
        //封装查询结果
        HashMap<String,Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("items",list);
        //返回
        return map;
    }

    @Override
    public void insert(DtsBrand brand) {
        brand.setAddTime(new Date());
        brand.setUpdateTime(new Date());
        brand.setDeleted(false);

        brandMapper.insert(brand);
    }

    @Override
    public void delete(DtsBrand brand) {
        brand.setUpdateTime(new Date());

        brandMapper.updateById(brand);
    }

    @Override
    public void update(DtsBrand brand) {
        //先将品牌设置为逻辑删除
        brand.setDeleted(true);

        brandMapper.updateById(brand);
    }

}
